package com.example.demo.common;

import com.example.demo.pojo.FileName;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class Nginx {
    public static String readConfigFile(String path) {
        try (BufferedReader reader = new BufferedReader(new FileReader(path))) {
            StringBuilder content = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                content.append(line).append("\n");
            }
            return content.toString();
        } catch (IOException e) {
            return "无法读取配置文件: " + e.getMessage();
        }
    }

    public static boolean writeConfigFile(String path, String content) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(path))) {
            writer.write(content);
            return true;
        } catch (IOException e) {
            return false;
        }
    }

    public static String testConfig() {
        try {
            Process process = new ProcessBuilder("sudo", "nginx", "-t").start();
            process.waitFor();
            return "配置正确！";
        } catch (Exception e) {
            return "配置错误: " + e.getMessage();
        }
    }

    public static void restartNginx() {
        try {
            Process process = new ProcessBuilder("sudo", "systemctl", "restart", "nginx").start();
            process.waitFor();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String getNginxStatus() {
        try {
            Process process = new ProcessBuilder("sudo", "systemctl", "status", "nginx").start();
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            StringBuilder status = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                status.append(line).append("\n");
            }
            return status.toString();
        } catch (IOException e) {
            return "无法获取 Nginx 状态: " + e.getMessage();
        }
    }


    public static String getNginxMainConf() {
        try {
            Process process = new ProcessBuilder("/bin/sh", "-c", "nginx -t 2>&1 | sed -n 's/.*configuration file \\(.*\\) test is successful/\\1/p'").start();
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            StringBuilder conf = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                conf.append(line);
            }
            return conf.toString();
        } catch (IOException e) {
            return "无法获取 Nginx 配置: " + e.getMessage();
        }
    }

    public static String getNginxDir() {
        try {
            Process process = new ProcessBuilder("/bin/sh", "-c", "nginx -t 2>&1 | sed -n 's/.*configuration file \\(.*\\/\\)[^/]* test is successful/\\1/p'").start();
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            StringBuilder conf = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                conf.append(line);
            }
            return conf.toString();
        } catch (IOException e) {
            return "无法获取 Nginx 目录: " + e.getMessage();
        }
    }


    public static List getNginxListDir(String path){
        try {
            Process process = new ProcessBuilder("ls", path).start();
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            List<String> outputLines = new ArrayList<>();
            while ((line = reader.readLine()) != null) {
                outputLines.add(line);
            }
            return outputLines;
        } catch (IOException e) {
            List<String> outputLines = new ArrayList<>();
            outputLines.add("1");
            return outputLines;
        }
    }

    public static void main(String[] args) {
        System.out.println(splitPath("/a/b"));
    }
    public static List<FileName> splitPath(String path) {
        List<FileName> result = new ArrayList<>();

        // 确保路径以 '/' 开头
        if (!path.startsWith("/")) {
            path = "/" + path;
        }

        if(path.length()==1){
            FileName fileName = new FileName();
            fileName.setName("/");
            fileName.setName("/");
            result.add(fileName);
            return result;
        }

        String[] parts = path.split("/");
        StringBuilder currentPath = new StringBuilder();

        for (String part : parts) {
            FileName fileName = new FileName();
            fileName.setName(part+"/");
            if (!part.isEmpty()) {
                currentPath.append("/").append(part);
                fileName.setPath(currentPath.toString());
                result.add(fileName);
            } else {
                fileName.setPath("/");
                result.add(fileName);
            }
        }

        if(result.size()>1){
            FileName fileName = result.get(result.size()-1);
            String name = fileName.getName();
            System.out.println(name);
            fileName.setName(name.replace("/",""));
        }
        return result;
    }

}
